<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <base data-ice="baseUrl" href="../../../">
  <title data-ice="title">src/public/js-iterators.js | jstreemap Library</title>
  <link type="text/css" rel="stylesheet" href="css/style.css">
  <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css">
  <script src="script/prettify/prettify.js"></script>
  <script src="script/manual.js"></script>
<meta name="description" content="Associative containers (sets, maps) library for JavaScript, using red-black trees."><meta property="twitter:card" content="summary"><meta property="twitter:title" content="jstreemap Library"><meta property="twitter:description" content="Associative containers (sets, maps) library for JavaScript, using red-black trees."></head>
<body class="layout-container" data-ice="rootContainer">

<header>
  <a href="./">Home</a>
  
  <a href="identifiers.html">Reference</a>
  <a href="source.html">Source</a>
  
  <div class="search-box">
  <span>
    <img src="./image/search.png">
    <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
  </span>
    <ul class="search-result"></ul>
  </div>
<a style="position:relative; top:3px;" href="https://github.com/Kirusi/jstreemap"><img width="20px" src="./image/github.png"></a></header>

<nav class="navigation" data-ice="nav"><div>
  <ul>
    
  <li data-ice="doc"><a data-ice="dirPath" class="nav-dir-path" href="identifiers.html#public">public</a><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/public/insertion-result.js~InsertionResult.html">InsertionResult</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/public/iterators.js~BaseIterator.html">BaseIterator</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/public/iterators.js~Iterator.html">Iterator</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/public/iterators.js~ReverseIterator.html">ReverseIterator</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/public/js-iterators.js~JsIterator.html">JsIterator</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/public/js-iterators.js~JsReverseIterator.html">JsReverseIterator</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/public/tree-map.js~TreeMap.html">TreeMap</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/public/tree-multimap.js~TreeMultiMap.html">TreeMultiMap</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/public/tree-multiset.js~TreeMultiSet.html">TreeMultiSet</a></span></span></li>
<li data-ice="doc"><span data-ice="kind" class="kind-class">C</span><span data-ice="name"><span><a href="class/src/public/tree-set.js~TreeSet.html">TreeSet</a></span></span></li>
</ul>
</div>
</nav>

<div class="content" data-ice="content"><h1 data-ice="title">src/public/js-iterators.js</h1>
<pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">&apos;use strict&apos;;

/* Containers are expected to support the following methods:
   jsBegin() - returns the very first node
   jsEnd() - returns the node beyond the last one
   next(node) - returns the next node
   prev(node) - returns the previous node
   valuePolicy - an instance of KeyOnlyPolicy, or KeyValuePolicy */
/**
  * ES6-style forward iterator.
  *
  * @example
  * let m = new TreeMap();
  * ...
  * for (let [key, value] of m) {
  *   console.log(`key: ${key}, value: ${value}`);
  * }
  * // iterate values
  * for (let value of m.values()) {
  *   console.log(`value: ${value}`);
  * }
  */
class JsIterator {
    /**
     * @param {*} container
     */
    constructor(container, valuePolicy = container.valuePolicy) {
        /**
         * @private
         * Internal reference to a container
         */
        this.container = container;
        /**
         * @private
         * valuePolicy implements what members of the node will be returned: key, value, or key and value
         */
        this.valuePolicy = valuePolicy;
        /**
         * @private
         * current node
         */
        this.node = container.jsBegin();
    }

    /**
     * As documented in ES6 iteration protocol. It can be used for manual iteration.
     * Iterators are documented here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
     *
     * @example
     * let m = new TreeMap();
     * ...
     * let jsIt = m.entries();
     * while (true) {
     *   let res = it.next();
     *   if (res.done) {
     *     break;
     *   }
     *   console.log(`key: ${res.value[0]}, value: ${res.value[1]`});
     * }
     */
    next() {
        let res = {};
        res.done = (this.node === this.container.jsEnd());
        if (!res.done) {
            res.value = this.valuePolicy.fetch(this.node);
            this.node = this.container.next(this.node);
        }
        return res;
    }

    /**
     * Support for ES6 for-of loops.
     * @returns {JsIterator}
     */
    [Symbol.iterator]() {
        return this;
    }

    /**
     * A reverse iterator for the same container.
     * @returns {JsReverseIterator}
     * @example
     * let m = new TreeMap();
     * ...
     * // iterate all key-value pairs in reverse order
     * for (let [key, value] of m.backwards()) {
     *   console.log(`key: ${key}, value: ${value}`);
     * }
    */
    backwards() {
        // eslint-disable-next-line no-use-before-define
        return new JsReverseIterator(this.container, this.valuePolicy);
    }
}

/* Containers are expected to support the following methods:
   jsRbegin() - returns the very first node in reverse order (e.g. the very last node)
   jsrEnd() - returns the node beyond the last one in reverse order (e.g. the node before the first one)
   next(node) - returns the next node
   prev(node) - returns the previous node
   valuePolicy - an instance of KeyOnlyPolicy, or KeyValuePolicy */
/**
  * ES6-style backward iterator
  * @example
  * let m = new TreeMap();
  * ...
  * // iterate all key-value pairs in reverse order
  * for (let [key, value] of m.backwards()) {
  *   console.log(`key: ${key}, value: ${value}`);
  * }
  * // iterate keys in reverse order
  * for (let key of m.keys().backwards()) {
  *   console.log(`key: ${key}`);
  * }
 */
class JsReverseIterator {
    /**
     * @param {*} container
     */
    constructor(container, valuePolicy = container.valuePolicy) {
        /**
         * @private
         * Internal reference to a container
         */
        this.container = container;
        /**
         * @private
         * valuePolicy implements what members of the node will be returned: key, value, or key and value
         */
        this.valuePolicy = valuePolicy;
        /**
         * @private
         * current node
         */
        this.node = container.jsRbegin();
    }

    /**
     * As documented in ES6 iteration protocol. It can be used for manual iteration.
     * Iterators are documented here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators
     *
     * @example
     * let m = new TreeMap();
     * ...
     * let jsIt = m.entries().backwards();
     * while (true) {
     *   let res = it.next();
     *   if (res.done) {
     *     break;
     *   }
     *   console.log(`key: ${res.value[0]}, value: ${res.value[1]`});
     * }
     */
    next() {
        let res = {};
        res.done = (this.node === this.container.jsRend());
        if (!res.done) {
            res.value = this.valuePolicy.fetch(this.node);
            this.node = this.container.prev(this.node);
        }
        return res;
    }

    /**
     * Support for ES6 for-of loops.
     * @returns {JsReverseIterator}
     */
    [Symbol.iterator]() {
        return this;
    }

    /**
     * A forward iterator for the same container
     * @returns {JsIterator}
     * @example
     * let m = new TreeMap();
     * ...
     * // iterate all key-value pairs in direct order
     * for (let [key, value] of m.backwards().backwards()) {
     *   console.log(`key: ${key}, value: ${value}`);
     */
    backwards() {
        return new JsIterator(this.container, this.valuePolicy);
    }
}

module.exports = {
    JsIterator: JsIterator,
    JsReverseIterator: JsReverseIterator
};</code></pre>

</div>

<footer class="footer">
  Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(1.0.4)</span><img src="./image/esdoc-logo-mini-black.png"></a>
</footer>

<script src="script/search_index.js"></script>
<script src="script/search.js"></script>
<script src="script/pretty-print.js"></script>
<script src="script/inherited-summary.js"></script>
<script src="script/test-summary.js"></script>
<script src="script/inner-link.js"></script>
<script src="script/patch-for-local.js"></script>
</body>
</html>
